package com.ebraga.trackerclub.android.manager.core;

import com.ebraga.trackerclub.android.dao.api.ParameterDao;
import com.ebraga.trackerclub.android.dao.api.PositionDao;
import com.ebraga.trackerclub.android.dao.entity.PositionAndroid;
import com.ebraga.trackerclub.android.dao.entity.UserAndroid;
import com.ebraga.trackerclub.android.exception.BackofficeDBConnectionException;
import com.ebraga.trackerclub.android.exception.dao.BackofficeDaoException;
import com.ebraga.trackerclub.android.exception.manager.BackofficeIllegalArgumentManagerException;
import com.ebraga.trackerclub.android.exception.manager.BackofficeManagerException;
import com.ebraga.trackerclub.android.manager.api.PositionManager;
import com.ebraga.trackerclub.android.util.DLog;

public abstract class PositionManagerImpl implements PositionManager {
	
	protected ParameterDao  parameterDao;
	protected PositionDao   positionDao;
	
	protected void setParameterDao (ParameterDao  parameterDao){
		this.parameterDao = parameterDao;
	}
	
	protected void setPositionDao (PositionDao   positionDao){
		this.positionDao = positionDao;
	}		

	public void sendPositionData(UserAndroid user, PositionAndroid position)
			throws BackofficeIllegalArgumentManagerException,
			BackofficeDBConnectionException,
			BackofficeManagerException {
		
		PositionAndroid  positionA; 
		
		if (user == null){
			throw new BackofficeIllegalArgumentManagerException("Usuario invalido");
		}
		
		if (user.getImei() == null){
			throw new BackofficeIllegalArgumentManagerException("Codigo do Usuario nulo");
		}
		
		if (position == null){
			throw new BackofficeIllegalArgumentManagerException("Posição invalida");
		}
		
		try{
			positionA = positionDao.getMaxSpeedByUser(user.getImei());
		}catch(BackofficeDBConnectionException db){
			DLog.e(db);
			throw new BackofficeDBConnectionException ("Erro ao conectar banco de dados");
		}catch(BackofficeDaoException e){
			DLog.e(e);
			throw new BackofficeManagerException (e.getMessage());
		}
		
		if (positionA == null || position.getSpeed() > positionA.getSpeed()){
			try {
				positionDao.insert(position);
			}catch(BackofficeDBConnectionException db){
				DLog.e(db);
				throw new BackofficeDBConnectionException ("Erro ao conectar banco de dados");
			} catch (BackofficeDaoException e) {
				DLog.e(e);
				throw new BackofficeManagerException(e.getMessage());
			}
		}
	}

	@Override
	public PositionAndroid getLastPosition(UserAndroid user)
			throws BackofficeIllegalArgumentManagerException,
			BackofficeDBConnectionException,
			BackofficeManagerException {
		
		PositionAndroid position; 
		
		if (user == null){
			throw new BackofficeIllegalArgumentManagerException("Usuario nulo");
		}
		
		if (user.getImei() == null){
			throw new BackofficeIllegalArgumentManagerException("Usuario invalido");
		}
		
		try {
			position = positionDao.getLastPositionbyUser(user.getImei());
		}catch(BackofficeDBConnectionException db){
			DLog.e(db);
			throw new  BackofficeDBConnectionException ("Erro ao conectar banco de dados");
		} catch (BackofficeDaoException e) {
			DLog.e(e);
			throw new BackofficeManagerException("Erro ao recuperar ultima posição do usuario");
		}
		
		return position;
	}

}
